#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "parser.h"

#define MAX 500
#define MAX_PATTERN_LEN 5

#define HTTP11		"http/1.1\n Free text\n\0"
#define HTTP10		"http/1.0\n Free text\n\0"
#define HTTP09		"http/0.9\n Free text\n\0"
#define NOT_HTTP	"not http/1.1\n Free text\n\0"


int get_input(char* input)
{
	int c, inputlen=0;
	
	for(c = 0; c < MAX; c++){
		// assumes there's plenty to eat
		input[inputlen] = getchar();
		inputlen++;
		if (input[inputlen-1] == '\n')
			break;
	}
	
	input[inputlen] = '\0';
	
	return inputlen;

}

int http_test(regexp* pattern, char* input, int inputlen)
{
	int c;	
	char* re = "^http/[01]\\.[091]";
	pattern = reg_compile(re);	
	return parser(pattern, input, inputlen);
}

int main()
{
	regexp * pattern = (regexp *)malloc(sizeof(struct regexp));
	char input[MAX];
	int inputlen = 0;

#ifdef free_test
	printf("Running free input test fot http pattern... \n");
	printf("=====================\n\n");
	printf("Enter input for http test\n");
	inputlen = get_input(input);
	if(http_test(pattern, input, inputlen))	puts("Match\n");
	else									puts("No match\n");
	
#else
	printf("Running http test... \n");
	printf("=====================\n\n");

	printf("Running http/1.1 test... \n");
	printf("-------------------------\n");
	printf(HTTP11);
	printf("--------\n");
	strcpy(input, HTTP11);
	inputlen = strlen(input);
	if(http_test(pattern, input, inputlen))	puts("Match\n");
	else									puts("No match\n");

	printf("Running http/1.0 test... \n");
	strcpy(input, HTTP10);
	printf("-------------------------\n");
	printf(HTTP10);
	printf("--------\n");
	inputlen = strlen(input);
	if(http_test(pattern, input, inputlen))	puts("Match\n");
	else									puts("No match\n");

	printf("Running http/0.9 test... \n");
	printf("-------------------------\n");
	printf(HTTP09);
	printf("--------\n");
	strcpy(input, HTTP09);
	inputlen = strlen(input);
	if(http_test(pattern, input, inputlen))	puts("Match\n");
	else									puts("No match\n");

	printf("Running not_http test... \n");
	printf("-------------------------\n");
	printf(NOT_HTTP);
	printf("--------\n");
	strcpy(input, NOT_HTTP);
	inputlen = strlen(input);
	if(http_test(pattern, input, inputlen))	puts("Match\n");
	else									puts("No match\n");
#endif

	return 0;

}
